/**
 * 
 */
package com.gf.pf.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gf.common.BaseController;
import com.gf.common.ResultMessage;
import com.gf.common.UUIDGenerator;
import com.gf.pf.model.Flexigrid;
import com.gf.pf.model.Role;
import com.gf.pf.model.RoleResource;
import com.gf.pf.model.Ztree;
import com.gf.pf.service.MyBatisService;
import com.gf.pf.service.RoleService;

/**
 * @description 角色管理
 * @date   Mar 4, 2015
 */
@Controller
@RequestMapping("/pf/role")
public class RoleController extends BaseController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);
	
	/**
	 * 角色列表
	 */
	private String ROLE_LIST = "pf/roleList";
	
	/**
	 * 添加角色
	 */
	private String ROLE_ADD = "pf/roleAdd";
	
	/**
	 * 修改角色
	 */
	private String ROLE_MODIFY = "pf/roleModify";
	
	/**
	 * 选择角色包含的职位
	 */
	private String CHOOSE_RESOURCE = "pf/chooseResource";
	
	/**
	 * 每页记录数
	 */
	private String PAGE_SQL_ID = "pf.role.roleList";
	
	/**
	 * 总记录数
	 */
	private String TOTAL_SQL_ID = "pf.role.total";
	
	/**
	 * 角色ID为空！
	 */
	private String role_id_empty = "角色ID为空！";
	
	@Autowired
	private RoleService roleService;

	@Autowired
	private MyBatisService myBatisService;
	
	/**
	 * 
	 * @description 角色管理列表
	 * @return String
	 */
	@RequestMapping(value = "/flRole", method = RequestMethod.GET)
	public String roleList() {
		return this.ROLE_LIST;
	}
	
	/**
	 * 
	 * @description 角色管理列表数据
	 * @return Flexigrid
	 */
	@RequestMapping(value = "/dataGrid", method = RequestMethod.POST)
	@ResponseBody
	public Flexigrid dataGrid(Flexigrid flex) {
		flex.setRowStart((flex.getPage() - 1) * flex.getRp());
		flex.setRowEnd(flex.getPage() * flex.getRp());
		List<?> rows = myBatisService.getList(this.PAGE_SQL_ID, flex);
		int total = myBatisService.count(this.TOTAL_SQL_ID, flex);
		flex.setTotal(total);
		flex.setRows(rows);
		return flex;
	}
	
	/**
	 * 
	 * @description 添加角色跳转
	 * @return String
	 */
	@RequestMapping(value = "/faRole", method = RequestMethod.GET)
	public String roleAddForward() {
		return this.ROLE_ADD;
	}
	
	/**
	 * 
	 * @description 添加角色
	 * @return String
	 */
	@RequestMapping(value = "/aRole", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage roleAdd(Role role) {
		role.setId(UUIDGenerator.getInstance().getUUID32());
		Integer result = roleService.saveRole(role);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("角色保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("角色保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 角色修改跳转
	 * @return String
	 */
	@RequestMapping(value = "/fmRole", method = RequestMethod.GET)
	public String roleModifyForward(String roleId, Model model) {
		if(!StringUtils.isEmpty(roleId)) {
			Role role = roleService.getRole(roleId);
			model.addAttribute("role", role);
		} else {
			LOGGER.error("角色ID为空！");
			return this.ROLE_LIST;
		}
		return this.ROLE_MODIFY;
	}
	
	/**
	 * 
	 * @description 角色修改
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/mRole", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage roleModify(Role role) {
		if(StringUtils.isEmpty(role.getId())) {
			LOGGER.error(this.role_id_empty);
		}
		
		Integer result = roleService.modifyRole(role);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("角色修改成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("角色修改失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 删除角色
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/dRole", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage roleDelete(String roleId) {
		if(StringUtils.isEmpty(roleId)) {
			LOGGER.error(this.role_id_empty);
		}
		
		Integer result = roleService.deleteRole(roleId);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("角色删除成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("职位删除失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 选择角色包含的职位
	 * @return String
	 */
	@RequestMapping(value = "/chooseResource", method = RequestMethod.GET)
	public String chooseResource(String roleId, Model model) {
		if(StringUtils.isEmpty(roleId)) {
			LOGGER.error(this.role_id_empty);
		}
		
		Role role = roleService.getRole(roleId);
		model.addAttribute("role", role);
		return this.CHOOSE_RESOURCE;
	}
	
	/**
	 * 
	 * @description 根据角色ID查询所包含的资源
	 * @return List<Ztree>
	 */
	@RequestMapping(value = "/roleResourceZtree", method = RequestMethod.POST)
	@ResponseBody
	public List<Ztree> getResourceByRole(String roleId) {
		return roleService.getResourceByRole(roleId);
	}
	
	/**
	 * 
	 * @description 保存角色所包含的资源
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/saveRoleResource", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage saveRoleResource(String roleId, String resourceIds) {
		if(StringUtils.isEmpty(roleId)) {
			LOGGER.error(this.role_id_empty);
		}
		
		if(StringUtils.isEmpty(resourceIds)) {
			LOGGER.error("资源IDS为空！");
		}
		
		Integer result = roleService.deleteRoleResource(roleId);
		if(result > 0) {
			LOGGER.error("删除角色(" + roleId + ")所包含资源成功！");
		} else {
			LOGGER.error("删除角色(" + roleId + ")所包含资源失败！");
		}
		
		String[] temp = resourceIds.split(",");
		Integer t = 1;
		for(int i = 0; i < temp.length; i++) {
			RoleResource rr = new RoleResource();
			rr.setRoleId(roleId);
			rr.setResourceId(temp[i]);
			Integer r = roleService.saveRoleResource(rr);
			t = t&r;
		}
		if(t == 1) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("保存角色所包含的资源成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("保存角色所包含的资源失败！");
		}
		return super.resultMessage;
	}
}
